Ext.ns("Morik.Office");
Morik.Office.CompanyPanel = function(config) {
	Morik.Office.CompanyPanel.superclass.constructor.call(this, config);
	var proxy = new Ext.data.DWRProxy( {
		api : {
			create : CompanyManager.addCompany,
			read : CompanyManager.getCompanys,
			update : CompanyManager.saveCompany,
			destroy : CompanyManager.removeCompany
		}
	});
	var recordType = new Ext.data.Record.create([]);
	var reader = new Ext.data.JsonReader( {}, recordType);
	var ds = new Ext.data.Store( {
		proxy : proxy,
		reader : reader,
		restful : true,
		writer : new Ext.data.DWRWriter()
	});
	this.ds = ds;
	var mp = this;
	ds.on("metachange", function(d, m) {
		if (m && Ext.isArray(m.fields)) {
			var f = m.fields, _heads = [];
			for (var i = 0; i < f.length; i++) {
				if (!f[i]._head) {
					continue;
				}
				var o = {};
				o.header = f[i]._head;
				o.dataIndex = f[i].name;
				o.editor = new Ext.form.TextField( {});
				_heads.push(o);
			}
		}
		var a=mp.grid;
		var ma=mp.grid.getColumnModel();
		mp.grid.getColumnModel().setConfig(_heads);
	});
	var cm = new Ext.grid.ColumnModel( {
		defaultSortable : true
	});

	var callback = function(r) {
		alert("success!");
	};
	var exception = function(r) {
		alert("failure!");
	};
	var update = function(t, r, type) {
		if (type == Ext.data.Record.COMMIT) {
			CompanyManager.saveCompany(r.data, {
				callback : callback,
				exceptionHandler : exception
			});
		}
	};
	var remove = function(t, r, i) {
		CompanyManager.removeCompany(r.data);
	};
	var buttonSave = new Ext.Button( {
		text : '保存',
		handler : function(e) {		
			ds.save();
		}
	});
	var buttonReset = new Ext.Button( {
		text : '复位',
		handler : function() {
			ds.rejectChanges();
		}
	});
	var buttonDelete = new Ext.Button( {
		text : '删除',
		handler : function(e) {
			var d = function(btn, text) {
				var sm = grid.getSelectionModel()
				if (btn == 'yes') {
					var rec = sm.getSelected();
					if (!rec)
						return;
					ds.remove(rec);
				}
			}
			Ext.MessageBox.confirm("确定删除", "是否真的要删除？", d);
		}
	});
	var t = this;
	var buttonNew = new Ext.Button( {
		text : '新加',
		handler : function(e) {
			var sm = grid.getSelectionModel();
			var rec = sm.getSelected();
			if (!rec)
				rec = ds.getAt(0);
			var c = rec.copy();			
			var s = new (recordType)(Ext.apply(c.data, config || {}));
			ds.insert(0, [s]);
			grid.stopEditing();
			grid.startEditing(0, 1);
		}
	});
	var pagingBar = new Ext.PagingToolbar( {
		pageSize : 10,
		store : ds,
		displayInfo : true,
		displayMsg : '共有 {2}，当前显示 {0} - {1}条',
		emptyMsg : "没有数据",
		items : ['-', buttonSave, '-', buttonDelete, '-', buttonNew, '-',
				buttonReset]
	});
	this.pagingBar = pagingBar;
	var sm = new Ext.grid.RowSelectionModel();

	var grid = new Ext.grid.EditorGridPanel( {
		cm : cm,
		sm : sm,
		store : ds,
		width : 680,
		height : 350,
		bbar : pagingBar,
		loadMask : {
			msg : '正在载入数据,请稍等...'
		},
		title : '公司列表'
	});
	this.grid=grid;
	this.add(grid);
		ds.load();	

}
Ext.extend(Morik.Office.CompanyPanel, Ext.Panel);
Ext.onReady(function() {
	var p = new Morik.Office.CompanyPanel();
	p.render(Ext.getBody());
});

/*
 * 
 * 
 */
